package com.jy.adscm.operation.pebase.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.pebase.bean.PeAttachment;
import com.jy.adscm.operation.pebase.bean.PePicture;
import com.jy.adscm.operation.pebase.service.PeAttachmentService;
import com.jy.adscm.operation.pebase.service.PePictureService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.system.utils.AdscmSystemUtils;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-08-19
 */
@RestController
@RequestMapping("/adscm/pebase/pePicture")
public class PePictureController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    PePictureService pePictureService;

    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody PePicture  pePicture , HttpServletRequest request) throws AdscmSystemException, IOException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        pePicture.setUploader(token.getUser().getId().toString());
        pePicture.setUploadTime(new Date());
        boolean insert = pePictureService.insert(pePicture);
        return new SimpleCoreResponse("200","新增成功",insert);
    }
    @RequestMapping("delete/{id}")
    public SimpleCoreResponse update(@PathVariable Integer  id , HttpServletRequest request) throws AdscmSystemException, IOException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        boolean insert = pePictureService.deleteById(id);
        return new SimpleCoreResponse("200","新增成功",insert);
    }
    @RequestMapping("selectByDid/{id}")
    public SimpleCoreResponse selectByDid(@PathVariable Integer  id , HttpServletRequest request) throws AdscmSystemException, IOException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
       List<PePicture>  pePictures= pePictureService.selectByDid(id);
        return new SimpleCoreResponse("200","新增成功",pePictures);
    }
}

